<template>
  <div>
    <el-card>
      <div style="display: flex; justify-content: space-between;">
        <el-statistic title="已报课程总数" :value="totalmc" />
        <el-input v-model="searchQuery" placeholder="搜索报课信息" style="width: 300px;" />
      </div>
    </el-card>
    <!-- 表格数据来源是后端查询的数据 -->
    <el-table :data="MC" style="width: 100%; margin-top: 20px;">
        <!-- prop用来获取data的键名 -->
      <el-table-column prop="memberNum" label="会员编号" />
      <el-table-column prop="memberName" label="会员姓名" />
      <el-table-column prop="courseNum" label="课程编号">
      </el-table-column>
      <el-table-column prop="courseName" label="课程名称" />
      <el-table-column prop="dt" label="办理时间" />
      <el-table-column label="操作">
        <!--  -->
        <template #default="scope">
          <el-button size="small" type="danger" @click="handleDelete(scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 编辑对话框 -->
    <!-- <el-dialog v-model="addmcDialogVisible" title="添加教练"> -->
      <!-- 编辑表单 -->
        <!-- <el-form ref="ruleFormRef" :model="ruleForm" :rules="rules"
      label-width="auto">
       <el-form-item label="教练姓名" prop="mcName"> 
    <el-input v-model="ruleForm.mcName" />
  </el-form-item>
  
     <el-form-item label="类型" prop="会籍顾问">
        <el-input v-model="ruleForm.mc"/>
  </el-form-item>
  
  <el-form-item label="电话号码" prop="phoneNum">
    <el-input v-model="ruleForm.phoneNum"/>
  </el-form-item>

      <el-form-item>
        <el-button type="primary" @click="submitForm(ruleFormRef)">
          确定
        </el-button>
        <el-button @click="resetForm(ruleFormRef)">
          清空
        </el-button>
      </el-form-item>
    </el-form>
    </el-dialog> -->
  </div>
</template>

<script setup lang="ts">
import { ref, computed, onMounted, reactive} from 'vue'
import axios from 'axios'
import { ElMessage, FormInstance, FormRules } from 'element-plus'
import { errorMessages } from 'vue/compiler-sfc'


// 定义响应式数组数据mcs
const mcs = ref([])
// 搜索会员响应数据
const searchQuery = ref('')

// 总会员数量 计算数组长度
const totalmc = computed(() => mcs.value.length)

const getAllmcs = (async()=>{
    // 获取mc表 /api/mcs是后端的相对地址
  const response = await axios.get('/api/MC')
  mcs.value = response.data
  console.log(mcs.value)

})

//过滤数据表 将需要的输出 
const MC = computed(() => {
    try{
    // 如果搜索的关键字的值为空 则返回所以 数组值
  if (!searchQuery.value) return mcs.value
// 否则返回搜索的  
  return mcs.value.filter(mc => 
//   通过课程名来过滤筛选
    mc.memberName.includes(searchQuery.value)||mc.courseName.includes(searchQuery.value) )
    }catch(error){
        console.log(errorMessages)
    }
})

// 挂载后端请求的数据
 onMounted(()=> {
    getAllmcs()
})

// // 删除课程
const handleDelete = async (mc) => {
  await axios.delete(`/api/MC/${mc.memberNum}/${mc.courseNum}`)
  ElMessage.success('删除成功!')
  console.log(mcs.value)
//   过滤数据
  mcs.value = mcs.value.filter(c => (c.memberNum !== mc.memberNum || c.courseNum !== mc.courseNum))
  
  // 再次渲染删除后的列表
  const update = await axios.get('/api/MC')
  mc.value = update.data

//   onMounted(() => {
//   handleDelete()
// })
}

</script>